<template>
    <div class="role">
        <Layout>
            <Header class="header">
                <Row>
                    <Col :span="24">
                        <div>
                            <span class="border-left"></span>
                            角色管理
                        </div>
                    </Col>
                </Row>
            </Header>
            <Content class="content">
                <div class="add-role">
                    <Row>
                        <Col :span="24" class="add-btn-box">
                            <div class="add-btn" @click="add">添加角色</div>
                        </Col>
                    </Row>
                </div>

                <div class="table-box">
                    <el-table
                        :data="tableData"
                        size="mini"
                        style="width: 100%;min-width:940px">
                        
                        <el-table-column
                            align="center"
                            prop="name"
                            label="角色名称">
                        </el-table-column>
                        <el-table-column
                            align="center"
                            prop="description"
                            label="职能描述"
                            width="200">
                        </el-table-column>
                        <el-table-column
                            align="center"
                            label="账号数量">
                            <template slot-scope="scope">
                                <p>{{scope.row.roleUsers}}</p>
                            </template>
                        </el-table-column>
                        <el-table-column
                            align="center"
                            label="添加时间">
                            <template slot-scope="scope">
                                <p>{{scope.row.create_time}}</p>
                            </template>
                        </el-table-column>
                        
                        <el-table-column
                            align="center"
                            label="是否启用">
                            <template slot-scope="scope">
                                <el-switch
                                    :value="scope.row.is_start === '1'"
                                    active-color="#13ce66"
                                    inactive-color="#ddd"
                                    @change="openChange(scope.row,true)">
                                </el-switch>
                            </template>
                        </el-table-column>

                        <el-table-column
                            label="操作"
                            width="200"
                            align="center">
                            <template slot-scope="scope">
                                <el-button type="text" size="small" @click="permissions(scope.row.id,scope.row.auth_ids,scope.row.name)">权限设置</el-button>
                                <el-button type="text" size="small" @click="edit(scope.row)">编辑</el-button>
                                <el-button type="text" size="small" @click="del(scope.row.id)" :disabled="scope.row.not_delete === '1'">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>

                <div class="page-box">
                    <Page :total="total" :page-size="pageSize" show-elevator show-total />
                </div>

                <!-- 添加角色模态框 -->
                <el-dialog
                    :title="roleTitle"
                    :visible.sync="addRole"
                    width="40%"
                    :before-close="cancel">
                    <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80" style="width: 60%;margin:auto">
                        <FormItem label="角色名称" prop="name">
                            <Input v-model="formValidate.name" placeholder="请填写角色名"></Input>
                        </FormItem>

                        <FormItem label="是否启用">
                            <i-switch :value="formValidate.is_start === '1' ? true : false" size="large" :disabled="formValidate.not_delete === '1'" @on-change="openChange(formValidate,false)">
                                <span slot="open">启用</span>
                                <span slot="close">关闭</span>
                            </i-switch>
                            <span v-show="formValidate.not_delete === '1'">(基础角色不可禁用)</span>
                        </FormItem>
                        
                        <FormItem label="角色描述" prop="description">
                            <Input v-model="formValidate.description" type="textarea" :autosize="{minRows: 4,maxRows: 6}" placeholder="请填写角色描述"></Input>
                        </FormItem>
                        <!-- <FormItem>
                            <Button type="primary" @click="handleSubmit('formValidate')">Submit</Button>
                            <Button @click="handleReset('formValidate')" style="margin-left: 8px">Reset</Button>
                        </FormItem> -->
                    </Form>
                    <span slot="footer" class="dialog-footer">
                        <el-button @click="cancel">取 消</el-button>
                        <el-button type="primary" @click="submit" :loading="submitLoading">确 定</el-button>
                    </span>
                </el-dialog>
            </Content>
            <Footer>
                <p class="copy-right">Copyright © {{host}}, All Rights Reserved.</p>
            </Footer>
        </Layout>
    </div>
</template>

<script>
    import * as api from '@/api/api';
    export default {
        data(){
            return {
                host: window.location.host,
                loading: this.$store.state,
                tableData: [],
                total: 6,
                pageSize: 10,
                current_page: 1,
                addRole: false,
                roleTitle: '',
                formValidate: {
                    name: '',
                    description: '',
                    is_start: '1',
                    not_delete: '0'
                },
                editId: '',//编辑id
                ruleValidate: {
                    name: [
                        { required: true, message: '请填写角色名', trigger: 'blur' }
                    ],
                },
                submitLoading: false,
            }
        },
        mounted(){
            this.getList();
        },

        computed: {

        },

        watch: {
            
        },
        
        methods: {
            getList() {
                let body = {
                    model: 'Roles',
                    with: {
                        0: 'roleUsers'
                    },
                    contUser: '',
                    offset: this.current_page -1,
                    limit: this.pageSize,
                    orderBy: '',
                    condition: {
                        is_delete: '0'
                    }
                };
                this.loading.isLoading = true;
                api.getDataList(body).then(res => {
                    if(res.data.code === 200){
                        this.loading.isLoading = false;
                        this.tableData = res.data.data.data
                    }
                })
            },
            add() {
                this.roleTitle = '添加角色';
                this.formValidate = {
                    name: '',
                    description: '',
                    is_start: '1'
                }
                this.addRole = true;
            },
            edit(row) {
                this.roleTitle = '编辑角色';
                this.editId = row.id;
                this.formValidate ={
                    name: row.name,
                    description: row.description,
                    is_start: row.is_start,
                    not_delete: row.not_delete,
                };
                this.addRole = true;
            },
            cancel() {
                this.addRole = false;
                this.submitLoading = false;
                this.$refs.formValidate.resetFields();
                this.formValidate = {
                    name: '',
                    description: '',
                    is_start: '1'
                }
            },
            del(id) {
                this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let body = {
                        model: 'Roles',
                        data: {
                            is_delete: '1'
                        },
                        update_condition: {
                            id
                        }
                    };
                    this.submitLoading = true;
                    api.createData(body).then(res => {
                        if(res.data.code === 200) {
                            this.getList();
                            this.$Message.success('删除成功！');
                        }
                    })
                })
            },
            submit() {
                if(this.roleTitle === '编辑角色'){
                    let body = {
                        model: 'Roles',
                        data: this.formValidate,
                        update_condition: {
                            id: this.editId
                        }
                    };
                    this.submitLoading = true;
                    api.createData(body).then(res => {
                        if(res.data.code === 200){
                            this.$Notice.success({
                                title: '提示',
                                desc: '恭喜你，修改成功！'
                            });
                            this.cancel();
                            this.getList();
                        }else{
                            this.$Notice.error({
                                title: '错误',
                                desc: '修改失败！'
                            });
                            this.cancel();
                        }
                    })
                }else{
                    let body = {
                        model: 'Roles',
                        data: this.formValidate
                    };
                    body.data.auth_ids = '3-1';
                    this.submitLoading = true;
                    api.createData(body).then(res => {
                        if(res.data.code === 200){
                            this.$Notice.success({
                                title: '提示',
                                desc: '恭喜你，添加成功！'
                            });
                            this.cancel();
                            this.getList();
                        }else{
                            this.$Notice.error({
                                title: '错误',
                                desc: '添加失败！'
                            });
                            this.cancel();
                        }
                    })
                }
            },
            //权限设置
            permissions(roleId,auth_ids,name) {
                this.$router.push({name: 'Permissions',params:{roleId,auth_ids,name}})
            },
            openChange(row,fast) {
                let old = row.is_start;
                if(row.not_delete === '1'){
                    this.$Notice.error({
                        title: '错误',
                        desc: '基础角色不可禁用'
                    });
                    row.is_start = old;
                    return false;
                }
                if(row.is_start === '1'){
                    row.is_start = '0'
                }else{
                    row.is_start = '1'
                }
                if(fast) {
                    let body = {
                        model: 'Roles',
                        data: {
                            is_start: row.is_start
                        },
                        update_condition: {
                            id: row.id
                        }
                    };
                    this.submitLoading = true;
                    api.createData(body).then(res => {
                        if(res.data.code === 200){
                            this.$Notice.success({
                                top: '150',
                                title: '提示',
                                desc: '恭喜你，修改成功！'
                            });
                        }else{
                            this.$Notice.error({
                                title: '错误',
                                desc: '修改失败'
                            });
                            row.is_start = old;
                        }
                    })
                }
            }
        }  
    }
</script>


<style lang="less">
    @import '../../../assets/styles/constant.less';
    .role{
        width: 100%;
        height: 100%;
        overflow-x: hidden;
        overflow-y: auto;
        background-color: #f5f7f9;
        .ivu-layout{
            min-height: 100%;
        }
        .header{
            font-size: 14px;
            line-height: 40px;
            height: 40px !important;
            color: #434343;
            padding: 0 50px;
            background-color: @background;
            border-bottom: 1px solid #ddd;
            .border-left {
                border-left: 5px solid @baseColor;
                margin-right: 4px;
            }
        }
        .content{
            width: 100%;
            min-height: 76%;
            padding: 20px 60px;
            .add-role{
                margin-top: 10px;
                .add-btn-box{
                    display: flex;
                    justify-content: flex-end;
                    .add-btn{
                        width: 100px;
                        height: 32px;
                        line-height: 32px;
                        text-align: center;
                        background-color: #fff;
                        border: 1px solid #ddd;
                        border-radius: 4px;
                        color: #333;
                        cursor: pointer;
                    }
                }
            }
            .table-box{
                margin-top: 10px;
                min-width: 100%;
                .el-table th{
                    background-color: #eee;
                    color: #333;
                    font-size: 14px;
                }
                .el-button--text{
                    color: @greenColor;
                }
            }
            .page-box{
                height: 50px;
                line-height: 50px;
                text-align: right;
                background-color: #fff;
                padding: 0 20px;
            }
        }
        .copy-right{
            text-align: center;
            color: #aaa;
            font-size: 12px;
        }
    }
</style>